/**
 * jims
 */
package com.jims.master.modules.patient.myFriend.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.myAttention.entity.PatFollow;
import com.jims.master.modules.patient.myFriend.dao.SysUserMessageDao;
import com.jims.master.modules.patient.myFriend.entity.SysUserFriend;
import com.jims.master.modules.patient.myFriend.entity.SysUserMessage;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.UserService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 发送消息Service
 * @author WangYQ
 * @version 2017-07-13
 */
@Service
@Transactional(readOnly = true)
public class SysUserMessageService extends CrudService<SysUserMessageDao, SysUserMessage> {

	@Autowired
	private SysUserFriendService sysUserFriendService;
	@Autowired
	private UserService userService;

	@Override
    public SysUserMessage get(String id) {
		return super.get(id);
	}
	
	@Override
    public List<SysUserMessage> findList(SysUserMessage sysUserMessage) {
		return super.findList(sysUserMessage);
	}
	
	@Override
    public Page<SysUserMessage> findPage(Page<SysUserMessage> page, SysUserMessage sysUserMessage) {
		return super.findPage(page, sysUserMessage);
	}
	/**
	 * 保存发送消息信息
	 * @param  faccid 网易好友id
	 * @param  message 发送的消息
	 * @return int
	 */
	@Transactional(readOnly = false)
	public int save(String account,String faccid ,String message) {
		SysUserFriend sysUserFriend=new SysUserFriend();
		User user =userService.findUserByAccount(account);
		user.setId(account);
		sysUserFriend.setFaccid(faccid);
		sysUserFriend.setUser(user);
		sysUserFriend=sysUserFriendService.findByFaccid(sysUserFriend);
		SysUserMessage sysUserMessage=new SysUserMessage();
		if(sysUserFriend!=null){
			sysUserMessage.setFriendId(sysUserFriend.getFriendId());
		}else {
			sysUserMessage.setFriendId(faccid);
		}
		sysUserMessage.setUser(user);
		sysUserMessage.setMessage(message);
		return super.save(sysUserMessage);
	}
	
	@Override
    @Transactional(readOnly = false)
	public int delete(SysUserMessage sysUserMessage) {
		return this.dao.delete(sysUserMessage);
	}

	public List<String> getPatient(String Id) {
		return dao.getPatient(Id);
	}
	/**
	 * 沟通过的患者
	 * @param  Id 用户id
	 * @param
	 * @return int
	 */
	public Page<SysUserMessage> communicationPatient(Page<SysUserMessage> page, Map map) {
		page.setPageNo(Integer.parseInt((String) map.get("pageNo")));
		if(StringUtils.isNotBlank((String) map.get("pageSize"))){
			page.setPageSize(Integer.parseInt((String) map.get("pageSize")));
		}else{
			page.setPageSize(10);
		}
		SysUserMessage sysUserMessage = new SysUserMessage();
		User user = new User();
		user.setId((String)map.get("id"));
		sysUserMessage.setUser(user);
		sysUserMessage.setPage(page);
		page.setList(this.dao.communicationPatient(sysUserMessage));
		return page;
	}
}